package app.modules.security.persistence.dao;

import app.modules.security.dto.web.resource.WebResourceDTO;
import app.modules.security.dto.web.resource.WebResourceQueryDTO;
import app.modules.security.persistence.mapper.ResourceMapper;
import app.modules.security.util.conversion.ResourceConvertUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jasmine.framework.common.util.CheckUtil;
import jasmine.framework.common.util.StringUtil;
import jasmine.framework.database.mybatisplus.dao.BaseI18nEntityDAO;
import jasmine.framework.database.mybatisplus.i18n.I18nEntityUtil;
import jasmine.framework.database.mybatisplus.wrapper.LambdaQueryWrapperEx;
import jasmine.framework.database.mybatisplus.wrapper.LambdaQueryWrapperWithI18n;
import jasmine.security.rbac.mapper.SecResourceMapper;
import jasmine.security.rbac.model.SecResource;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author mh.z
 */
@Repository
public class ResourceDAO extends BaseI18nEntityDAO<ResourceMapper, SecResource> {
    private SecResourceMapper secResourceMapper;

    public ResourceDAO(SecResourceMapper secResourceMapper) {
        this.secResourceMapper = secResourceMapper;
    }

    /**
     * 分页查询资源
     *
     * @param query
     * @param page
     * @return
     */
    public List<WebResourceDTO> pageWebResourceDTOsByCond(WebResourceQueryDTO query, Page page) {
        CheckUtil.notNull(query, "query null");
        CheckUtil.notNull(page, "page null");
        LambdaQueryWrapperWithI18n<SecResource> wrapper = LambdaQueryWrapperWithI18n.wrapper();

        String resourcePath = query.getResourcePath();
        wrapper.like(StringUtil.isNotEmpty(resourcePath), SecResource::getResourcePath, resourcePath);
        String remark = query.getRemark();
        wrapper.likeWithI18n(StringUtil.isNotEmpty(remark), SecResource::getRemark, remark);
        wrapper.orderByAsc(SecResource::getResourcePath);

        Page<SecResource> pageResult = baseMapper.selectPageWithI18n(page, wrapper);
        List<SecResource> recordList = pageResult.getRecords();
        List<WebResourceDTO> returnList = ResourceConvertUtil.secResource2WebResourceDTO(recordList);

        return returnList;
    }

}
